<template>
  <el-dialog title="改款工单" :visible.sync="visible" width="800px">
    <div>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="是否补差价" prop="fall">
          <el-switch
            v-model="form.fall"
            :active-value="1"
            :inactive-value="0"
            active-text="是"
            inactive-text="否"></el-switch>
        </el-form-item>
        <el-form-item v-show="form.fall === 1" label="补差价单号" prop="reason">
          <el-input class="w-300" v-model.trim="form.reason" placeholder="请填写补差价单号"></el-input>
        </el-form-item>
        <el-form-item v-show="form.fall === 0" label="原因" prop="reason">
          <el-input type="textarea" v-model.trim="form.reason" placeholder="请填写原因"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit()">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  updateWorkGoodsSku, // 新增催单
} from "@/api/workorder";
export default {
  props: {
    ruleForm: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    const resultValidator = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.form.fall === 1 ? '请填写单号' : '请填写原因'));
      } else {
        callback();
      }
    }
    return {
      visible: false,
      source: 2,// 1:异常补单 2:修改订单 3:确认订单
      workOrderId: '',
      orderData: {},
      form: {
        fall: 0,
        reason: "", 
      },
      rules: {
        reason: [
          { required: true, validator: resultValidator, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    show({workOrderId, orderData, source} = {}) {
      this.visible = true;
      this.source = source
      this.workOrderId = workOrderId;
      this.orderData = orderData || {};
      this.initForm();
    },
    initForm() {
      this.form = {
        fall: 0,
        reason: ''
      }
    },
    // 确认按钮
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          updateWorkGoodsSku({
            id: this.workOrderId,
            fall: this.form.fall,
            reason: this.form.reason,
            orderId: this.orderData.id,
          }).then((res) => {
            if (res.data.code == 200) {
              this.visible = false;
              // this.$message({
              //   type: 'success',
              //   message: '提交成功！'
              // });
              this.$emit('submit');
            }
          });
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped></style>